/*
 * PlayerDeathEvent.java
 * Team qq 2011
 */
package com.google.code.timetrail.backend;

import java.util.List;
import java.util.Random;

/**
 * Player death random event
 * 
 * @author Utkarsh
 * @version 1.1
 */
public class PlayerDeathEvent extends Event {
	/**
	 * PlayerDeathEvent's SerialVersion ID, to be changed when the object is
	 * changed
	 **/
	private static final long serialVersionUID = 5020L;

	/**
	 * The randomly selected person to potentially die
	 */
	private final Person randomDeadPerson;

	/**
	 * The instance of the game to be acted upon
	 */
	private final Control gameControl;

	/**
	 * A List of the players in the party
	 */
	private final List<Person> players;

	/**
	 * the index of the person to be removed
	 */
	private final int removedPersonIndex;

	/**
	 * Value 100
	 */
	private static final int VALUE_HUNDRED = 100;

	/**
	 * Takes a random person, and sets their status to Dead, the sucess of this
	 * is dependant upon whether or not the person is sick, as well as their
	 * swimming skill
	 * 
	 * @param gameControl
	 *            the instance of the game ot be acted upon
	 */
	public PlayerDeathEvent(Control gameControl) {
		this.gameControl = gameControl;
		final Random rand = new Random();
		players = gameControl.getMembers();
		if (players.size() != 1) {
			removedPersonIndex = rand.nextInt(players.size() - 1) + 1;
			randomDeadPerson = players.get(removedPersonIndex);
			if (!randomDeadPerson.isDead()
					&& randomDeadPerson.isSick()
					&& randomDeadPerson.getDeath() < rand.nextDouble()
					&& randomDeadPerson.getSkills()[0] < rand
							.nextInt(VALUE_HUNDRED)) {
				super.formatMessage(randomDeadPerson
						+ " has died in the time stream.");
			} else {
				super.messageText = getDummyText();
			}
		} else {
			super.messageText = getDummyText();
			removedPersonIndex = 0;
			randomDeadPerson = players.get(removedPersonIndex);
		}
	}

	/**
	 * The action for this event
	 */
	@Override
	public void action() {
		players.remove(removedPersonIndex);
		randomDeadPerson.die();
		players.add(randomDeadPerson);
		gameControl.setMembers(players);
	}
}
